<?php

namespace Beerus\Modules\Designer\Actions;

use Beerus\Modules\Designer\Controllers\KaDesignerController;
use Doraemon\service\KaManage\Combo;
use Doraemon\tools\Log;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Doraemon\model\KaDesigner;
use Doraemon\model\Member as mMember;
use Doraemon\model\DesignerAmountRecord;

class CollectAmountRecord extends RowAction
{

    public $name = '收款录入';

    public function handle($id,Request $request)
    {
        //处理收款录入逻辑
        $contract_number = $request->get('contract_number');
        $uid = $request->get('uid');
        $amount = $request->get('collection_amount');
        $amount_time = $request->get('collection_time');
        $serial_number = $request->get('serial_number');
        //已收款金额
        $received_amount = $request->get('received_amount');
        //合同金额
        $contract_amount = $request->get('contract_amount');
        $data = [
            'uid' => $uid,
            'contract_number' => $contract_number,
            'amount' => $amount,
            'amount_time' => strtotime($amount_time),
            'serial_number' => $serial_number,
            'addtime' => time()
        ];
        //判断已收款金额和本次收款金额的合与合同金额的关系
        $have_amount = floatval($received_amount) + floatval(sprintf("%.2f",$amount));
        if ($have_amount > floatval($contract_amount)){
            return $this->response()->error('累计收款金额超出合同金额，请检查后重新提交');
        }elseif ($have_amount == floatval($contract_amount)){
            //如果金额恰好相等,将订单自动设置为生效
            // 如果订单已生效则不再更新data_status字段
            $kaOrderInfo = KaDesigner::getKaOrderInfo($id);
            if ($kaOrderInfo['data_status'] != KaDesigner::STATUS_EFFECT) {
                $update_data['data_status'] = KaDesigner::STATUS_EFFECT;
                KaDesigner::updateById($id,$update_data);
                //增加子账号权益
                KaDesignerController::CreatEquity($id);
            }
        }
        //录入收款记录
        $result = DesignerAmountRecord::create($data);
        if ($result){
            // 初始化设计师KA广告套餐信息
            if ((!empty($update_data['data_status']) && $update_data['data_status'] == KaDesigner::STATUS_EFFECT) && !Combo::initDesignerComboDetail($id)) {
                Log::info("设计师KA广告套餐信息初始化失败，订单ID：{$id}");
            }
            return $this->response()->success('收款已成功录入(＾－＾)V')->refresh();
        }else{
            return $this->response()->success('收款录入失败,请稍后重试!');
        }

    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function form($request)
    {
        $id = $request->id;
        //获取该id的相关合同信息
        $contract_info = KaDesigner::getById($id);
        //获取用户的基础信息
        $uid = $contract_info['uid'];
        $user_info = mMember::getUserInfo($uid);
        $nick = $user_info['nick'];
        //合同基础信息
        $customer_name = $contract_info['customer_name'];
        $contract_amount = $contract_info['contract_amount'];
        $contract_number = $contract_info['contract_number'];
        //根据合同编号获取所有收款记录
        $received_amount = self::getAllRecAmount($contract_number);

        $this->text('nick','用户昵称')->default($nick)->readonly();
        $this->text('customer_name','客户名称')->default($customer_name)->readonly();
        $this->text('contract_amount','合同金额')->default(sprintf("%.2f",$contract_amount))->readonly();
        $this->text('received_amount','已收款')->default($received_amount)->readonly();
        $this->text('contract_number','合同编号')->default($contract_number)->readonly();
        $this->hidden('uid')->value($uid);
        $this->date('collection_time','收款时间');
        $this->text('collection_amount', '收款金额')->required();
        $this->text('serial_number', '银行流水号')->required();
    }

    private static function getAllRecAmount($contract_number){
        $condition['contract_number'] = $contract_number;
        $record_info = DesignerAmountRecord::getAllByCondition($condition);
        $all_amount = 0;
        if ($record_info){
            foreach ($record_info as $value){
                $all_amount = $all_amount + $value['amount'];
            }
        }
        return sprintf("%.2f",$all_amount);
    }

}
